#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pip install prettytable
import urllib.request as req
import json


# 上传函数
def OneNet_post_data(info):
    url = "https://api.heclouds.com/devices/" + info[0] + '/datapoints'
    headers = {'api-key': info[1]}
    streams = []
    for i, e in enumerate(info[2]):
        streams.append({"id": e[0], "datapoints": [{"value": e[1]}]})
    data = json.dumps({"datastreams": streams}).encode("utf-8")
    request = json.loads((req.urlopen(req.Request(url, data, headers=headers))).read())
    return request


# 获取函数
def OneNet_get_data(info):
    url = "http://api.heclouds.com/devices/" + info[0] + "/datastreams"
    headers = {'api-key': info[1]}
    request = json.loads((req.urlopen(req.Request(url, headers=headers))).read())
    data = request.pop('data')
    return data, request
